package com.wclass.mapper;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.entity.CourseInfo;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 课程基本信息 Mapper 接口
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-21
 */
public interface CourseInfoMapper extends BaseMapper<CourseInfo> {

    /**
     * <p>
     * 根据 entity 条件，查询全部记录（并翻页）
     * </p>
     *
     * @param rowBounds 分页查询条件（可以为 RowBounds.DEFAULT）
     * @param wrapper   实体对象封装操作类（可以为 null）
     * @return List<T>
     */
    @Override
    @Select("select info.*,baseAppUser.nick_name,baseAppUser.picture_address,baseAppUser.level as user_level," +
            "baseAppUser.postname,baseAppUser.personal_intro,baseAppUser.course_num,baseAppUser.student_num ${ew.sqlSelect} from course_info as info " +
            "left join base_app_user as baseAppUser on info.user_id=baseAppUser.id " +
            "where info.state=0 and baseAppUser.state=0 " +
            "${ew.sqlSegment}")
    List<CourseInfo> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<CourseInfo> wrapper);

    @Select("select info.*,baseAppUser.nick_name,baseAppUser.picture_address,baseAppUser.level as user_level," +
            "baseAppUser.postname,baseAppUser.personal_intro,baseAppUser.course_num,baseAppUser.student_num" +
            ",(select MAX(bill.crt_time) from coures_progress_bill bill where bill.course_info_id = info.id) as latest_study_time " +
            "${ew.sqlSelect} " +
            "from course_info as info " +
            "left join base_app_user as baseAppUser on info.user_id=baseAppUser.id " +
            "left join coures_order as courseOrder on courseOrder.course_info_id=info.id " +
            "where info.state=0 and baseAppUser.state=0 " +
            "${ew.sqlSegment}")
    List<CourseInfo> selectCourseWithOrderPage(Page<CourseInfo> page, @Param("ew") Wrapper<CourseInfo> wrapper);

    /**
     * <p>
     * 根据 ID 查询
     * </p>
     *
     * @param id 主键ID
     * @return T
     */
    @Override
    @Select("select info.*,baseAppUser.nick_name,baseAppUser.picture_address,baseAppUser.level as user_level," +
            "baseAppUser.postname,baseAppUser.personal_intro,baseAppUser.course_num,baseAppUser.student_num " +
            "from course_info as info " +
            "left join base_app_user as baseAppUser on info.user_id=baseAppUser.id " +
            "where info.state=0 and baseAppUser.state=0 and info.id=#{id}")
    CourseInfo selectById(@Param("id") Serializable id);

}
